var express = require("express");
var router = express.Router();
const moment = require("moment");
const accountModel = require("../../models/accountsModel");
const { default: mongoose } = require("mongoose");

/* // 测试
console.log(moment("2024-04-09").toDate()); */

// 记账本列表
router.get("/account", function (req, res, next) {
  console.log(req.session.username);
  if (!req.session.username) {
    return res.redirect("/auth/login");
  }
  accountModel
    .find()
    .sort({
      time: -1,
    })
    .exec((err, data) => {
      if (err) {
        res.json({
          code: "1001",
          msg: "获取账单列表失败~~",
          data: null,
        });
        return;
      }
      console.log(moment(data.time).format("YYYY-MM-DD"));
      res.json({
        code: "0000",
        msg: "获取账单列表成功~~",
        data: data,
      });
    });
});

// 添加记录
router.post("/account", (req, res) => {
  console.log(req.body);
  accountModel.create(
    {
      ...req.body,
      time: moment(req.body.time).toDate(),
    },
    (err, data) => {
      if (err) {
        // console.log(err);
        res.json({
          code: "1002",
          msg: "添加账单失败~~",
          data: null,
        });
        return;
      }
      console.log(data);
      res.json({
        code: "0000",
        msg: "添加账单成功~~",
        data: data,
      });
    },
  );
});

// 删除记录
router.delete("/account/:id", (req, res) => {
  // 获取 id
  let id = req.params.id;

  accountModel.deleteOne(
    {
      _id: id,
    },
    (err, data) => {
      if (err) {
        res.json({
          code: "1003",
          msg: "删除账单失败~~",
          data: null,
        });
        return;
      }
      // 提醒
      res.json({
        code: "0000",
        msg: "删除成功",
        data: {},
      });
    },
  );
});

// 获取单个账单
router.get("/account/:id", (req, res) => {
  // 获取 id
  let id = req.params.id;

  // 删除 id 对应的账单
  /* accountModel.findOne(
    {
      _id: id,
    },
    (err, data) => {
      if (err) {
        res.json({
          code: "1004",
          msg: "获取单个账单失败~~",
          data: null,
        });
        return;
      }
      res.json({
        code: "0000",
        msg: "获取单个账单成功~~",
        data: data,
      });
    },
  ); */
  accountModel.findById(id, (err, data) => {
    if (err) {
      res.json({
        code: "1004",
        msg: "获取单个账单失败",
        data: null,
      });
      return;
    }
    res.json({
      code: "0000",
      msg: "获取单个账单成功",
      data: data,
    });
  });
});

// 更新单个账单
router.patch("/account/:id", (req, res) => {
  // 获取账单 id
  let { id } = req.params;

  // 更新 id 对应的账单
  accountModel.findByIdAndUpdate(
    id,
    req.body,
    {
      new: true,
    },
    (err, data) => {
      if (err) {
        return res.json({
          code: "1005",
          msg: "更新单个账单失败~~",
          data: null,
        });
      }
      return res.json({
        code: "0000",
        msg: "更新单个账单成功",
        data: data,
      });
    },
  );
  /* accountModel.updateOne(
    {
      _id: id,
    },
    req.body,
    (err, data) => {
      if (err) {
        return res.json({
          code: "1005",
          msg: "更新账单部分内容失败~~",
          data: null,
        });
      }
      return res.json({
        code: "0000",
        msg: "更新账单部分内容成功",
        data: data,
      });
    },
  ); */
});

module.exports = router;
